using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//参数控制器
namespace ApiProject.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class SpecificationController : ControllerBase
    {
        private IRepository<Specifications> _specificationRepository;
        private IRepository<SpecificationChildrens> _spechildRepository;

        public SpecificationController(

            IRepository<Specifications> specificationRepository,
            IRepository<SpecificationChildrens> spechildRepository
        )
        {
            _specificationRepository = specificationRepository;
            _spechildRepository = spechildRepository;
        }

        //添加参数
        [HttpPost]
        public dynamic AddPartentSpe(NewSpe model)
        {
            var spename = model.SpecificationName.Trim();
            if (string.IsNullOrEmpty(spename))
            {
                return JsonHelper
                        .Serialize(new
                        {
                            Code = 1000,
                            Data = "",
                            Msg = "名称不能为空"
                        });

            }
            else
            {


                var allSpeChild = _spechildRepository.Table.Where(x => x.SpchildrenName == spename && x.IsDelete == false).FirstOrDefault();

                var allSpe = _specificationRepository.Table.Where(x => x.SpecificationName == spename && x.IsDelete == false).FirstOrDefault();
                if (allSpe != null || allSpeChild != null)
                {
                    return JsonHelper
                            .Serialize(new
                            {
                                Code = 1000,
                                Data = "",
                                Msg = "该名称已存在"
                            });
                }
                else
                {
                    var spe = new Specifications
                    {
                        SpecificationName = spename
                    };

                    _specificationRepository.Insert(spe);

                    return JsonHelper
                            .Serialize(new
                            {
                                Code = 200,
                                Data = spe,
                                Msg = "添加成功"
                            });
                }
            }



        }

        //获取所有（父级）参数
        [HttpGet]
        public dynamic GetAllSpe()
        {
            var spe = _specificationRepository.Table.Where(x => x.IsDelete == false).ToList();
            return JsonHelper
                    .Serialize(new
                    {
                        Code = 200,
                        Data = spe,
                        Msg = "获取成功"
                    });
        }

        //添加子级参数
        [HttpPost, Route("spechild")]
        public dynamic AddSpeChild(NewSpeChild model)
        {
            var spechildname = model.SpchildrenName.Trim();
            var partid = model.PartId;
            var allSpe = _specificationRepository.Table.Where(x => x.SpecificationName == spechildname && x.IsDelete == false).FirstOrDefault();
            var allSpeChild = _spechildRepository.Table.Where(x => x.SpchildrenName == spechildname && x.IsDelete == false).FirstOrDefault();
            if (allSpe != null || allSpeChild != null)
            {
                return JsonHelper
                        .Serialize(new
                        {
                            Code = 1000,
                            Data = "",
                            Msg = "该名称已存在"
                        });
            }
            else
            {
                var spechild = new SpecificationChildrens
                {
                    SpchildrenName = spechildname,
                    SpecificationId = partid
                };
                _spechildRepository.Insert(spechild);
                return JsonHelper
                        .Serialize(new
                        {
                            Code = 200,
                            Data = spechild,
                            Msg = "添加成功"
                        });
            }


        }
        //查找所有子级参数

        [HttpGet, Route("spechild")]
        public dynamic GetAllSpeChild()
        {
            var spechild = _spechildRepository.Table.Where(x => x.IsDelete == false).ToList();

            return JsonHelper
                    .Serialize(new
                    {
                        Code = 200,
                        Data = spechild,
                        Msg = "获取成功"
                    });
        }

        //伪删除父类参数
        [HttpDelete("{id}")]
        public dynamic DeleteSpe(int id)
        {
            var Spe = _specificationRepository.Table.Where(x => x.Id == id && x.IsDelete == false).FirstOrDefault();

            if (Spe != null)
            {
                //删除父类连同子类一起删除
                var speChild = _spechildRepository.Table.Where(x => x.SpecificationId == id && x.IsDelete == false).ToList();
                speChild.ForEach(item =>
                {
                    item.IsDelete = true;
                    item.IsEnable = false;
                    item.UpdateTime=DateTime.Now;
                    _spechildRepository.Update(item);

                });


                Spe.IsDelete = true;
                Spe.IsEnable = false;
                Spe.UpdateTime = DateTime.Now;
                _specificationRepository.Update(Spe);
                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = Spe,
                    Msg = "删除成功"
                });

            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "删除失败"
                });
            }

        }

        //伪删除子类参数
        [HttpPost, Route("delete")]
        public dynamic DeleteSpeChild(NewSpe model)
        {
            var spechileName = model.SpecificationName.Trim();
            var speChild = _spechildRepository.Table.Where(x => x.SpchildrenName == spechileName && x.IsDelete == false).FirstOrDefault();


            if (speChild != null)
            {

                speChild.IsDelete = true;
                speChild.IsEnable = false;
                speChild.UpdateTime = DateTime.Now;
                _spechildRepository.Update(speChild);
                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = speChild,
                    Msg = "删除成功"
                });

            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "删除失败"
                });
            }

        }

        //编辑父级分类
        [HttpPut]
        public dynamic EditSpe(NewEditSpe model)
        {
            var spename = model.SpecificationName.Trim();
            var speId = model.Id;
            if (string.IsNullOrEmpty(spename))
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "传入的名称不为空"
                });

            }
            else
            {

                var Spe = _specificationRepository.Table.Where(x => x.Id == speId && x.IsDelete == false).FirstOrDefault();
                if (Spe != null)
                {
                    Spe.SpecificationName = spename;

                    _specificationRepository.Update(Spe);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = Spe,
                        Msg = "修改成功"
                    });
                }
                else
                {
                    return JsonHelper.Serialize(new
                    {
                        Code = 1000,
                        Data = "",
                        Msg = "修改失败"
                    });
                }








            }
        }



    }
}
